{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4f12b390-1078-4e3e-9d05-3bd1f11ee7ba",
   "metadata": {},
   "source": [
    "## 2.2 Tokenizing text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "9ca72cd4-e62f-4fc0-8c5a-9b2af83016cb",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 获取训练需要的语料\n",
    "import os \n",
    "import urllib.request\n",
    "\n",
    "if not os.path.exists(\"the-verdict.txt\"):\n",
    "    url = (\"https://raw.githubusercontent.com/rasbt/LLMs-from-scratch/refs/heads/main/ch02/01_main-chapter-code/the-verdict.txt\")\n",
    "    file_path=\"the-verdict.txt\"\n",
    "    urllib.request.urlretrieve(url, file_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "5d84f941-49a5-4855-8c41-f56bc95c2a29",
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(\"the-verdict.txt\", \"r\", encoding=\"utf-8\") as f:\n",
    "    raw_text = f.read()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "66bbe308-9e8c-4d48-ad63-2d4d278d8e45",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'I HAD always thought Jack Gisburn rather a cheap genius--though a good fellow enough--so it was no great surprise to me to hear that, in the height of his glory, he had dropped his painting, married a rich widow, and established himself in a villa on the Riviera. (Though I rather thought it would have been Rome or Florence.)\\n\\n\"The height of his glory\"--that was what the women called it. I can hear Mrs. Gideon Thwing--his last Chicago sitter--deploring his unaccountable abdication. \"Of course it\\'s going to send the value of my picture \\'way up; but I don\\'t think of that, Mr. Rickham--the loss to Arrt is all I think of.\" The word, on Mrs. Thwing\\'s lips, multiplied its _rs_ as though they were reflected in an endless vista of mirrors. And it was not only the Mrs. Thwings who mourned. Had not the exquisite Hermia Croft, at the last Grafton Gallery show, stopped me before Gisburn\\'s \"Moon-dancers\" to say, with tears in her eyes: \"We shall not look upon its like again\"?\\n\\nWell!--even through the prism of Hermia\\'s tears I felt able to face the fact with equanimity. Poor Jack Gisburn! The women had made him--it was fitting that they should mourn him. Among his own sex fewer regrets were heard, and in his own trade hardly a murmur. Professional jealousy? Perhaps. If it were, the honour of the craft was vindicated by little Claude Nutley, who, in all good faith, brought out in the Burlington a very handsome \"obituary\" on Jack--one of those showy articles stocked with random technicalities that I have heard (I won\\'t say by whom) compared to Gisburn\\'s painting. And so--his resolve being apparently irrevocable--the discussion gradually died out, and, as Mrs. Thwing had predicted, the price of \"Gisburns\" went up.\\n\\nIt was not till three years later that, in the course of a few weeks\\' idling on the Riviera, it suddenly occurred to me to wonder why Gisburn had given up his painting. On reflection, it really was a tempting problem. To accuse his wife would have been too easy--his fair sitters had been denied the solace of saying that Mrs. Gisburn had \"dragged him down.\" For Mrs. Gisburn--as such--had not existed till nearly a year after Jack\\'s resolve had been taken. It might be that he had married her--since he liked his ease--because he didn\\'t want to go on painting; but it would have been hard to prove that he had given up his painting because he had married her.\\n\\nOf course, if she had not dragged him down, she had equally, as Miss Croft contended, failed to \"lift him up\"--she had not led him back to the easel. To put the brush into his hand again--what a vocation for a wife! But Mrs. Gisburn appeared to have disdained it--and I felt it might be interesting to find out why.\\n\\nThe desultory life of the Riviera lends itself to such purely academic speculations; and having, on my way to Monte Carlo, caught a glimpse of Jack\\'s balustraded terraces between the pines, I had myself borne thither the next day.\\n\\nI found the couple at tea beneath their palm-trees; and Mrs. Gisburn\\'s welcome was so genial that, in the ensuing weeks, I claimed it frequently. It was not that my hostess was \"interesting\": on that point I could have given Miss Croft the fullest reassurance. It was just because she was _not_ interesting--if I may be pardoned the bull--that I found her so. For Jack, all his life, had been surrounded by interesting women: they had fostered his art, it had been reared in the hot-house of their adulation. And it was therefore instructive to note what effect the \"deadening atmosphere of mediocrity\" (I quote Miss Croft) was having on him.\\n\\nI have mentioned that Mrs. Gisburn was rich; and it was immediately perceptible that her husband was extracting from this circumstance a delicate but substantial satisfaction. It is, as a rule, the people who scorn money who get most out of it; and Jack\\'s elegant disdain of his wife\\'s big balance enabled him, with an appearance of perfect good-breeding, to transmute it into objects of art and luxury. To the latter, I must add, he remained relatively indifferent; but he was buying Renaissance bronzes and eighteenth-century pictures with a discrimination that bespoke the amplest resources.\\n\\n\"Money\\'s only excuse is to put beauty into circulation,\" was one of the axioms he laid down across the Sevres and silver of an exquisitely appointed luncheon-table, when, on a later day, I had again run over from Monte Carlo; and Mrs. Gisburn, beaming on him, added for my enlightenment: \"Jack is so morbidly sensitive to every form of beauty.\"\\n\\nPoor Jack! It had always been his fate to have women say such things of him: the fact should be set down in extenuation. What struck me now was that, for the first time, he resented the tone. I had seen him, so often, basking under similar tributes--was it the conjugal note that robbed them of their savour? No--for, oddly enough, it became apparent that he was fond of Mrs. Gisburn--fond enough not to see her absurdity. It was his own absurdity he seemed to be wincing under--his own attitude as an object for garlands and incense.\\n\\n\"My dear, since I\\'ve chucked painting people don\\'t say that stuff about me--they say it about Victor Grindle,\" was his only protest, as he rose from the table and strolled out onto the sunlit terrace.\\n\\nI glanced after him, struck by his last word. Victor Grindle was, in fact, becoming the man of the moment--as Jack himself, one might put it, had been the man of the hour. The younger artist was said to have formed himself at my friend\\'s feet, and I wondered if a tinge of jealousy underlay the latter\\'s mysterious abdication. But no--for it was not till after that event that the _rose Dubarry_ drawing-rooms had begun to display their \"Grindles.\"\\n\\nI turned to Mrs. Gisburn, who had lingered to give a lump of sugar to her spaniel in the dining-room.\\n\\n\"Why _has_ he chucked painting?\" I asked abruptly.\\n\\nShe raised her eyebrows with a hint of good-humoured surprise.\\n\\n\"Oh, he doesn\\'t _have_ to now, you know; and I want him to enjoy himself,\" she said quite simply.\\n\\nI looked about the spacious white-panelled room, with its _famille-verte_ vases repeating the tones of the pale damask curtains, and its eighteenth-century pastels in delicate faded frames.\\n\\n\"Has he chucked his pictures too? I haven\\'t seen a single one in the house.\"\\n\\nA slight shade of constraint crossed Mrs. Gisburn\\'s open countenance. \"It\\'s his ridiculous modesty, you know. He says they\\'re not fit to have about; he\\'s sent them all away except one--my portrait--and that I have to keep upstairs.\"\\n\\nHis ridiculous modesty--Jack\\'s modesty about his pictures? My curiosity was growing like the bean-stalk. I said persuasively to my hostess: \"I must really see your portrait, you know.\"\\n\\nShe glanced out almost timorously at the terrace where her husband, lounging in a hooded chair, had lit a cigar and drawn the Russian deerhound\\'s head between his knees.\\n\\n\"Well, come while he\\'s not looking,\" she said, with a laugh that tried to hide her nervousness; and I followed her between the marble Emperors of the hall, and up the wide stairs with terra-cotta nymphs poised among flowers at each landing.\\n\\nIn the dimmest corner of her boudoir, amid a profusion of delicate and distinguished objects, hung one of the familiar oval canvases, in the inevitable garlanded frame. The mere outline of the frame called up all Gisburn\\'s past!\\n\\nMrs. Gisburn drew back the window-curtains, moved aside a _jardiniere_ full of pink azaleas, pushed an arm-chair away, and said: \"If you stand here you can just manage to see it. I had it over the mantel-piece, but he wouldn\\'t let it stay.\"\\n\\nYes--I could just manage to see it--the first portrait of Jack\\'s I had ever had to strain my eyes over! Usually they had the place of honour--say the central panel in a pale yellow or _rose Dubarry_ drawing-room, or a monumental easel placed so that it took the light through curtains of old Venetian point. The more modest place became the picture better; yet, as my eyes grew accustomed to the half-light, all the characteristic qualities came out--all the hesitations disguised as audacities, the tricks of prestidigitation by which, with such consummate skill, he managed to divert attention from the real business of the picture to some pretty irrelevance of detail. Mrs. Gisburn, presenting a neutral surface to work on--forming, as it were, so inevitably the background of her own picture--had lent herself in an unusual degree to the display of this false virtuosity. The picture was one of Jack\\'s \"strongest,\" as his admirers would have put it--it represented, on his part, a swelling of muscles, a congesting of veins, a balancing, straddling and straining, that reminded one of the circus-clown\\'s ironic efforts to lift a feather. It met, in short, at every point the demand of lovely woman to be painted \"strongly\" because she was tired of being painted \"sweetly\"--and yet not to lose an atom of the sweetness.\\n\\n\"It\\'s the last he painted, you know,\" Mrs. Gisburn said with pardonable pride. \"The last but one,\" she corrected herself--\"but the other doesn\\'t count, because he destroyed it.\"\\n\\n\"Destroyed it?\" I was about to follow up this clue when I heard a footstep and saw Jack himself on the threshold.\\n\\nAs he stood there, his hands in the pockets of his velveteen coat, the thin brown waves of hair pushed back from his white forehead, his lean sunburnt cheeks furrowed by a smile that lifted the tips of a self-confident moustache, I felt to what a degree he had the same quality as his pictures--the quality of looking cleverer than he was.\\n\\nHis wife glanced at him deprecatingly, but his eyes travelled past her to the portrait.\\n\\n\"Mr. Rickham wanted to see it,\" she began, as if excusing herself. He shrugged his shoulders, still smiling.\\n\\n\"Oh, Rickham found me out long ago,\" he said lightly; then, passing his arm through mine: \"Come and see the rest of the house.\"\\n\\nHe showed it to me with a kind of naive suburban pride: the bath-rooms, the speaking-tubes, the dress-closets, the trouser-presses--all the complex simplifications of the millionaire\\'s domestic economy. And whenever my wonder paid the expected tribute he said, throwing out his chest a little: \"Yes, I really don\\'t see how people manage to live without that.\"\\n\\nWell--it was just the end one might have foreseen for him. Only he was, through it all and in spite of it all--as he had been through, and in spite of, his pictures--so handsome, so charming, so disarming, that one longed to cry out: \"Be dissatisfied with your leisure!\" as once one had longed to say: \"Be dissatisfied with your work!\"\\n\\nBut, with the cry on my lips, my diagnosis suffered an unexpected check.\\n\\n\"This is my own lair,\" he said, leading me into a dark plain room at the end of the florid vista. It was square and brown and leathery: no \"effects\"; no bric-a-brac, none of the air of posing for reproduction in a picture weekly--above all, no least sign of ever having been used as a studio.\\n\\nThe fact brought home to me the absolute finality of Jack\\'s break with his old life.\\n\\n\"Don\\'t you ever dabble with paint any more?\" I asked, still looking about for a trace of such activity.\\n\\n\"Never,\" he said briefly.\\n\\n\"Or water-colour--or etching?\"\\n\\nHis confident eyes grew dim, and his cheeks paled a little under their handsome sunburn.\\n\\n\"Never think of it, my dear fellow--any more than if I\\'d never touched a brush.\"\\n\\nAnd his tone told me in a flash that he never thought of anything else.\\n\\nI moved away, instinctively embarrassed by my unexpected discovery; and as I turned, my eye fell on a small picture above the mantel-piece--the only object breaking the plain oak panelling of the room.\\n\\n\"Oh, by Jove!\" I said.\\n\\nIt was a sketch of a donkey--an old tired donkey, standing in the rain under a wall.\\n\\n\"By Jove--a Stroud!\" I cried.\\n\\nHe was silent; but I felt him close behind me, breathing a little quickly.\\n\\n\"What a wonder! Made with a dozen lines--but on everlasting foundations. You lucky chap, where did you get it?\"\\n\\nHe answered slowly: \"Mrs. Stroud gave it to me.\"\\n\\n\"Ah--I didn\\'t know you even knew the Strouds. He was such an inflexible hermit.\"\\n\\n\"I didn\\'t--till after. . . . She sent for me to paint him when he was dead.\"\\n\\n\"When he was dead? You?\"\\n\\nI must have let a little too much amazement escape through my surprise, for he answered with a deprecating laugh: \"Yes--she\\'s an awful simpleton, you know, Mrs. Stroud. Her only idea was to have him done by a fashionable painter--ah, poor Stroud! She thought it the surest way of proclaiming his greatness--of forcing it on a purblind public. And at the moment I was _the_ fashionable painter.\"\\n\\n\"Ah, poor Stroud--as you say. Was _that_ his history?\"\\n\\n\"That was his history. She believed in him, gloried in him--or thought she did. But she couldn\\'t bear not to have all the drawing-rooms with her. She couldn\\'t bear the fact that, on varnishing days, one could always get near enough to see his pictures. Poor woman! She\\'s just a fragment groping for other fragments. Stroud is the only whole I ever knew.\"\\n\\n\"You ever knew? But you just said--\"\\n\\nGisburn had a curious smile in his eyes.\\n\\n\"Oh, I knew him, and he knew me--only it happened after he was dead.\"\\n\\nI dropped my voice instinctively. \"When she sent for you?\"\\n\\n\"Yes--quite insensible to the irony. She wanted him vindicated--and by me!\"\\n\\nHe laughed again, and threw back his head to look up at the sketch of the donkey. \"There were days when I couldn\\'t look at that thing--couldn\\'t face it. But I forced myself to put it here; and now it\\'s cured me--cured me. That\\'s the reason why I don\\'t dabble any more, my dear Rickham; or rather Stroud himself is the reason.\"\\n\\nFor the first time my idle curiosity about my companion turned into a serious desire to understand him better.\\n\\n\"I wish you\\'d tell me how it happened,\" I said.\\n\\nHe stood looking up at the sketch, and twirling between his fingers a cigarette he had forgotten to light. Suddenly he turned toward me.\\n\\n\"I\\'d rather like to tell you--because I\\'ve always suspected you of loathing my work.\"\\n\\nI made a deprecating gesture, which he negatived with a good-humoured shrug.\\n\\n\"Oh, I didn\\'t care a straw when I believed in myself--and now it\\'s an added tie between us!\"\\n\\nHe laughed slightly, without bitterness, and pushed one of the deep arm-chairs forward. \"There: make yourself comfortable--and here are the cigars you like.\"\\n\\nHe placed them at my elbow and continued to wander up and down the room, stopping now and then beneath the picture.\\n\\n\"How it happened? I can tell you in five minutes--and it didn\\'t take much longer to happen. . . . I can remember now how surprised and pleased I was when I got Mrs. Stroud\\'s note. Of course, deep down, I had always _felt_ there was no one like him--only I had gone with the stream, echoed the usual platitudes about him, till I half got to think he was a failure, one of the kind that are left behind. By Jove, and he _was_ left behind--because he had come to stay! The rest of us had to let ourselves be swept along or go under, but he was high above the current--on everlasting foundations, as you say.\\n\\n\"Well, I went off to the house in my most egregious mood--rather moved, Lord forgive me, at the pathos of poor Stroud\\'s career of failure being crowned by the glory of my painting him! Of course I meant to do the picture for nothing--I told Mrs. Stroud so when she began to stammer something about her poverty. I remember getting off a prodigious phrase about the honour being _mine_--oh, I was princely, my dear Rickham! I was posing to myself like one of my own sitters.\\n\\n\"Then I was taken up and left alone with him. I had sent all my traps in advance, and I had only to set up the easel and get to work. He had been dead only twenty-four hours, and he died suddenly, of heart disease, so that there had been no preliminary work of destruction--his face was clear and untouched. I had met him once or twice, years before, and thought him insignificant and dingy. Now I saw that he was superb.\\n\\n\"I was glad at first, with a merely aesthetic satisfaction: glad to have my hand on such a \\'subject.\\' Then his strange life-likeness began to affect me queerly--as I blocked the head in I felt as if he were watching me do it. The sensation was followed by the thought: if he _were_ watching me, what would he say to my way of working? My strokes began to go a little wild--I felt nervous and uncertain.\\n\\n\"Once, when I looked up, I seemed to see a smile behind his close grayish beard--as if he had the secret, and were amusing himself by holding it back from me. That exasperated me still more. The secret? Why, I had a secret worth twenty of his! I dashed at the canvas furiously, and tried some of my bravura tricks. But they failed me, they crumbled. I saw that he wasn\\'t watching the showy bits--I couldn\\'t distract his attention; he just kept his eyes on the hard passages between. Those were the ones I had always shirked, or covered up with some lying paint. And how he saw through my lies!\\n\\n\"I looked up again, and caught sight of that sketch of the donkey hanging on the wall near his bed. His wife told me afterward it was the last thing he had done--just a note taken with a shaking hand, when he was down in Devonshire recovering from a previous heart attack. Just a note! But it tells his whole history. There are years of patient scornful persistence in every line. A man who had swum with the current could never have learned that mighty up-stream stroke. . . .\\n\\n\"I turned back to my work, and went on groping and muddling; then I looked at the donkey again. I saw that, when Stroud laid in the first stroke, he knew just what the end would be. He had possessed his subject, absorbed it, recreated it. When had I done that with any of my things? They hadn\\'t been born of me--I had just adopted them. . . .\\n\\n\"Hang it, Rickham, with that face watching me I couldn\\'t do another stroke. The plain truth was, I didn\\'t know where to put it--_I had never known_. Only, with my sitters and my public, a showy splash of colour covered up the fact--I just threw paint into their faces. . . . Well, paint was the one medium those dead eyes could see through--see straight to the tottering foundations underneath. Don\\'t you know how, in talking a foreign language, even fluently, one says half the time not what one wants to but what one can? Well--that was the way I painted; and as he lay there and watched me, the thing they called my \\'technique\\' collapsed like a house of cards. He didn\\'t sneer, you understand, poor Stroud--he just lay there quietly watching, and on his lips, through the gray beard, I seemed to hear the question: \\'Are you sure you know where you\\'re coming out?\\'\\n\\n\"If I could have painted that face, with that question on it, I should have done a great thing. The next greatest thing was to see that I couldn\\'t--and that grace was given me. But, oh, at that minute, Rickham, was there anything on earth I wouldn\\'t have given to have Stroud alive before me, and to hear him say: \\'It\\'s not too late--I\\'ll show you how\\'?\\n\\n\"It _was_ too late--it would have been, even if he\\'d been alive. I packed up my traps, and went down and told Mrs. Stroud. Of course I didn\\'t tell her _that_--it would have been Greek to her. I simply said I couldn\\'t paint him, that I was too moved. She rather liked the idea--she\\'s so romantic! It was that that made her give me the donkey. But she was terribly upset at not getting the portrait--she did so want him \\'done\\' by some one showy! At first I was afraid she wouldn\\'t let me off--and at my wits\\' end I suggested Grindle. Yes, it was I who started Grindle: I told Mrs. Stroud he was the \\'coming\\' man, and she told somebody else, and so it got to be true. . . . And he painted Stroud without wincing; and she hung the picture among her husband\\'s things. . . .\"\\n\\nHe flung himself down in the arm-chair near mine, laid back his head, and clasping his arms beneath it, looked up at the picture above the chimney-piece.\\n\\n\"I like to fancy that Stroud himself would have given it to me, if he\\'d been able to say what he thought that day.\"\\n\\nAnd, in answer to a question I put half-mechanically--\"Begin again?\" he flashed out. \"When the one thing that brings me anywhere near him is that I knew enough to leave off?\"\\n\\nHe stood up and laid his hand on my shoulder with a laugh. \"Only the irony of it is that I _am_ still painting--since Grindle\\'s doing it for me! The Strouds stand alone, and happen once--but there\\'s no exterminating our kind of art.\"'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "raw_text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "297a9d4a-8104-4425-a835-f1fedd604541",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Hello,', ' ', 'World,', ' ', 'Here,', ' ', 'is,', ' ', 'Irelia.']\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "text=\"Hello, World, Here, is, Irelia.\"\n",
    "result = re.split(r'(\\s)', text)\n",
    "\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "71d0ab75-a394-46f8-ade7-6aff342d6cd6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Hello', ',', '', ' ', 'World', ',', '', ' ', 'Here', ',', '', ' ', 'is', ',', '', ' ', 'Irelia', '.', '']\n"
     ]
    }
   ],
   "source": [
    "result = re.split(r'([,.]|\\s)', text)\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "f5174e9f-2159-4e72-8e51-6444dcc06531",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Hello', ',', 'World', ',', 'Here', ',', 'is', ',', 'Irelia', '.']\n"
     ]
    }
   ],
   "source": [
    "result = [item for item in result if item.strip()]\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "db3797c8-bf27-472c-8af4-9cfb2ca0c10e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 特殊字符分割\n",
    "result = re.split(r'([,.:;?_!\"()\\']|--|\\s)', raw_text)\n",
    "result = [item for item in result if item.strip()]\n",
    "preprocessed=result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "9874fc6b-0e38-41b2-823c-ad523064fdf5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4690"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(preprocessed)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "0aa0a05d-371b-4cbc-8233-d0b7bb0c34b3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['I',\n",
       " 'HAD',\n",
       " 'always',\n",
       " 'thought',\n",
       " 'Jack',\n",
       " 'Gisburn',\n",
       " 'rather',\n",
       " 'a',\n",
       " 'cheap',\n",
       " 'genius']"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "preprocessed[:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "2db1c5d7-3985-4705-ba73-f27ec86f4c23",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1130"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 去重排序\n",
    "all_words = sorted(set(preprocessed))\n",
    "len(all_words)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "8fca03c8-b74b-480b-a73c-5b04f5a0cd1b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'!': 0,\n",
       " '\"': 1,\n",
       " \"'\": 2,\n",
       " '(': 3,\n",
       " ')': 4,\n",
       " ',': 5,\n",
       " '--': 6,\n",
       " '.': 7,\n",
       " ':': 8,\n",
       " ';': 9,\n",
       " '?': 10,\n",
       " 'A': 11,\n",
       " 'Ah': 12,\n",
       " 'Among': 13,\n",
       " 'And': 14,\n",
       " 'Are': 15,\n",
       " 'Arrt': 16,\n",
       " 'As': 17,\n",
       " 'At': 18,\n",
       " 'Be': 19,\n",
       " 'Begin': 20,\n",
       " 'Burlington': 21,\n",
       " 'But': 22,\n",
       " 'By': 23,\n",
       " 'Carlo': 24,\n",
       " 'Chicago': 25,\n",
       " 'Claude': 26,\n",
       " 'Come': 27,\n",
       " 'Croft': 28,\n",
       " 'Destroyed': 29,\n",
       " 'Devonshire': 30,\n",
       " 'Don': 31,\n",
       " 'Dubarry': 32,\n",
       " 'Emperors': 33,\n",
       " 'Florence': 34,\n",
       " 'For': 35,\n",
       " 'Gallery': 36,\n",
       " 'Gideon': 37,\n",
       " 'Gisburn': 38,\n",
       " 'Gisburns': 39,\n",
       " 'Grafton': 40,\n",
       " 'Greek': 41,\n",
       " 'Grindle': 42,\n",
       " 'Grindles': 43,\n",
       " 'HAD': 44,\n",
       " 'Had': 45,\n",
       " 'Hang': 46,\n",
       " 'Has': 47,\n",
       " 'He': 48,\n",
       " 'Her': 49,\n",
       " 'Hermia': 50,\n",
       " 'His': 51,\n",
       " 'How': 52,\n",
       " 'I': 53,\n",
       " 'If': 54,\n",
       " 'In': 55,\n",
       " 'It': 56,\n",
       " 'Jack': 57,\n",
       " 'Jove': 58,\n",
       " 'Just': 59,\n",
       " 'Lord': 60,\n",
       " 'Made': 61,\n",
       " 'Miss': 62,\n",
       " 'Money': 63,\n",
       " 'Monte': 64,\n",
       " 'Moon-dancers': 65,\n",
       " 'Mr': 66,\n",
       " 'Mrs': 67,\n",
       " 'My': 68,\n",
       " 'Never': 69,\n",
       " 'No': 70,\n",
       " 'Now': 71,\n",
       " 'Nutley': 72,\n",
       " 'Of': 73,\n",
       " 'Oh': 74,\n",
       " 'On': 75,\n",
       " 'Once': 76,\n",
       " 'Only': 77,\n",
       " 'Or': 78,\n",
       " 'Perhaps': 79,\n",
       " 'Poor': 80,\n",
       " 'Professional': 81,\n",
       " 'Renaissance': 82,\n",
       " 'Rickham': 83,\n",
       " 'Riviera': 84,\n",
       " 'Rome': 85,\n",
       " 'Russian': 86,\n",
       " 'Sevres': 87,\n",
       " 'She': 88,\n",
       " 'Stroud': 89,\n",
       " 'Strouds': 90,\n",
       " 'Suddenly': 91,\n",
       " 'That': 92,\n",
       " 'The': 93,\n",
       " 'Then': 94,\n",
       " 'There': 95,\n",
       " 'They': 96,\n",
       " 'This': 97,\n",
       " 'Those': 98,\n",
       " 'Though': 99,\n",
       " 'Thwing': 100,\n",
       " 'Thwings': 101,\n",
       " 'To': 102,\n",
       " 'Usually': 103,\n",
       " 'Venetian': 104,\n",
       " 'Victor': 105,\n",
       " 'Was': 106,\n",
       " 'We': 107,\n",
       " 'Well': 108,\n",
       " 'What': 109,\n",
       " 'When': 110,\n",
       " 'Why': 111,\n",
       " 'Yes': 112,\n",
       " 'You': 113,\n",
       " '_': 114,\n",
       " 'a': 115,\n",
       " 'abdication': 116,\n",
       " 'able': 117,\n",
       " 'about': 118,\n",
       " 'above': 119,\n",
       " 'abruptly': 120,\n",
       " 'absolute': 121,\n",
       " 'absorbed': 122,\n",
       " 'absurdity': 123,\n",
       " 'academic': 124,\n",
       " 'accuse': 125,\n",
       " 'accustomed': 126,\n",
       " 'across': 127,\n",
       " 'activity': 128,\n",
       " 'add': 129,\n",
       " 'added': 130,\n",
       " 'admirers': 131,\n",
       " 'adopted': 132,\n",
       " 'adulation': 133,\n",
       " 'advance': 134,\n",
       " 'aesthetic': 135,\n",
       " 'affect': 136,\n",
       " 'afraid': 137,\n",
       " 'after': 138,\n",
       " 'afterward': 139,\n",
       " 'again': 140,\n",
       " 'ago': 141,\n",
       " 'ah': 142,\n",
       " 'air': 143,\n",
       " 'alive': 144,\n",
       " 'all': 145,\n",
       " 'almost': 146,\n",
       " 'alone': 147,\n",
       " 'along': 148,\n",
       " 'always': 149,\n",
       " 'am': 150,\n",
       " 'amazement': 151,\n",
       " 'amid': 152,\n",
       " 'among': 153,\n",
       " 'amplest': 154,\n",
       " 'amusing': 155,\n",
       " 'an': 156,\n",
       " 'and': 157,\n",
       " 'another': 158,\n",
       " 'answer': 159,\n",
       " 'answered': 160,\n",
       " 'any': 161,\n",
       " 'anything': 162,\n",
       " 'anywhere': 163,\n",
       " 'apparent': 164,\n",
       " 'apparently': 165,\n",
       " 'appearance': 166,\n",
       " 'appeared': 167,\n",
       " 'appointed': 168,\n",
       " 'are': 169,\n",
       " 'arm': 170,\n",
       " 'arm-chair': 171,\n",
       " 'arm-chairs': 172,\n",
       " 'arms': 173,\n",
       " 'art': 174,\n",
       " 'articles': 175,\n",
       " 'artist': 176,\n",
       " 'as': 177,\n",
       " 'aside': 178,\n",
       " 'asked': 179,\n",
       " 'at': 180,\n",
       " 'atmosphere': 181,\n",
       " 'atom': 182,\n",
       " 'attack': 183,\n",
       " 'attention': 184,\n",
       " 'attitude': 185,\n",
       " 'audacities': 186,\n",
       " 'away': 187,\n",
       " 'awful': 188,\n",
       " 'axioms': 189,\n",
       " 'azaleas': 190,\n",
       " 'back': 191,\n",
       " 'background': 192,\n",
       " 'balance': 193,\n",
       " 'balancing': 194,\n",
       " 'balustraded': 195,\n",
       " 'basking': 196,\n",
       " 'bath-rooms': 197,\n",
       " 'be': 198,\n",
       " 'beaming': 199,\n",
       " 'bean-stalk': 200,\n",
       " 'bear': 201,\n",
       " 'beard': 202,\n",
       " 'beauty': 203,\n",
       " 'became': 204,\n",
       " 'because': 205,\n",
       " 'becoming': 206,\n",
       " 'bed': 207,\n",
       " 'been': 208,\n",
       " 'before': 209,\n",
       " 'began': 210,\n",
       " 'begun': 211,\n",
       " 'behind': 212,\n",
       " 'being': 213,\n",
       " 'believed': 214,\n",
       " 'beneath': 215,\n",
       " 'bespoke': 216,\n",
       " 'better': 217,\n",
       " 'between': 218,\n",
       " 'big': 219,\n",
       " 'bits': 220,\n",
       " 'bitterness': 221,\n",
       " 'blocked': 222,\n",
       " 'born': 223,\n",
       " 'borne': 224,\n",
       " 'boudoir': 225,\n",
       " 'bravura': 226,\n",
       " 'break': 227,\n",
       " 'breaking': 228,\n",
       " 'breathing': 229,\n",
       " 'bric-a-brac': 230,\n",
       " 'briefly': 231,\n",
       " 'brings': 232,\n",
       " 'bronzes': 233,\n",
       " 'brought': 234,\n",
       " 'brown': 235,\n",
       " 'brush': 236,\n",
       " 'bull': 237,\n",
       " 'business': 238,\n",
       " 'but': 239,\n",
       " 'buying': 240,\n",
       " 'by': 241,\n",
       " 'called': 242,\n",
       " 'came': 243,\n",
       " 'can': 244,\n",
       " 'canvas': 245,\n",
       " 'canvases': 246,\n",
       " 'cards': 247,\n",
       " 'care': 248,\n",
       " 'career': 249,\n",
       " 'caught': 250,\n",
       " 'central': 251,\n",
       " 'chair': 252,\n",
       " 'chap': 253,\n",
       " 'characteristic': 254,\n",
       " 'charming': 255,\n",
       " 'cheap': 256,\n",
       " 'check': 257,\n",
       " 'cheeks': 258,\n",
       " 'chest': 259,\n",
       " 'chimney-piece': 260,\n",
       " 'chucked': 261,\n",
       " 'cigar': 262,\n",
       " 'cigarette': 263,\n",
       " 'cigars': 264,\n",
       " 'circulation': 265,\n",
       " 'circumstance': 266,\n",
       " 'circus-clown': 267,\n",
       " 'claimed': 268,\n",
       " 'clasping': 269,\n",
       " 'clear': 270,\n",
       " 'cleverer': 271,\n",
       " 'close': 272,\n",
       " 'clue': 273,\n",
       " 'coat': 274,\n",
       " 'collapsed': 275,\n",
       " 'colour': 276,\n",
       " 'come': 277,\n",
       " 'comfortable': 278,\n",
       " 'coming': 279,\n",
       " 'companion': 280,\n",
       " 'compared': 281,\n",
       " 'complex': 282,\n",
       " 'confident': 283,\n",
       " 'congesting': 284,\n",
       " 'conjugal': 285,\n",
       " 'constraint': 286,\n",
       " 'consummate': 287,\n",
       " 'contended': 288,\n",
       " 'continued': 289,\n",
       " 'corner': 290,\n",
       " 'corrected': 291,\n",
       " 'could': 292,\n",
       " 'couldn': 293,\n",
       " 'count': 294,\n",
       " 'countenance': 295,\n",
       " 'couple': 296,\n",
       " 'course': 297,\n",
       " 'covered': 298,\n",
       " 'craft': 299,\n",
       " 'cried': 300,\n",
       " 'crossed': 301,\n",
       " 'crowned': 302,\n",
       " 'crumbled': 303,\n",
       " 'cry': 304,\n",
       " 'cured': 305,\n",
       " 'curiosity': 306,\n",
       " 'curious': 307,\n",
       " 'current': 308,\n",
       " 'curtains': 309,\n",
       " 'd': 310,\n",
       " 'dabble': 311,\n",
       " 'damask': 312,\n",
       " 'dark': 313,\n",
       " 'dashed': 314,\n",
       " 'day': 315,\n",
       " 'days': 316,\n",
       " 'dead': 317,\n",
       " 'deadening': 318,\n",
       " 'dear': 319,\n",
       " 'deep': 320,\n",
       " 'deerhound': 321,\n",
       " 'degree': 322,\n",
       " 'delicate': 323,\n",
       " 'demand': 324,\n",
       " 'denied': 325,\n",
       " 'deploring': 326,\n",
       " 'deprecating': 327,\n",
       " 'deprecatingly': 328,\n",
       " 'desire': 329,\n",
       " 'destroyed': 330,\n",
       " 'destruction': 331,\n",
       " 'desultory': 332,\n",
       " 'detail': 333,\n",
       " 'diagnosis': 334,\n",
       " 'did': 335,\n",
       " 'didn': 336,\n",
       " 'died': 337,\n",
       " 'dim': 338,\n",
       " 'dimmest': 339,\n",
       " 'dingy': 340,\n",
       " 'dining-room': 341,\n",
       " 'disarming': 342,\n",
       " 'discovery': 343,\n",
       " 'discrimination': 344,\n",
       " 'discussion': 345,\n",
       " 'disdain': 346,\n",
       " 'disdained': 347,\n",
       " 'disease': 348,\n",
       " 'disguised': 349,\n",
       " 'display': 350,\n",
       " 'dissatisfied': 351,\n",
       " 'distinguished': 352,\n",
       " 'distract': 353,\n",
       " 'divert': 354,\n",
       " 'do': 355,\n",
       " 'doesn': 356,\n",
       " 'doing': 357,\n",
       " 'domestic': 358,\n",
       " 'don': 359,\n",
       " 'done': 360,\n",
       " 'donkey': 361,\n",
       " 'down': 362,\n",
       " 'dozen': 363,\n",
       " 'dragged': 364,\n",
       " 'drawing-room': 365,\n",
       " 'drawing-rooms': 366,\n",
       " 'drawn': 367,\n",
       " 'dress-closets': 368,\n",
       " 'drew': 369,\n",
       " 'dropped': 370,\n",
       " 'each': 371,\n",
       " 'earth': 372,\n",
       " 'ease': 373,\n",
       " 'easel': 374,\n",
       " 'easy': 375,\n",
       " 'echoed': 376,\n",
       " 'economy': 377,\n",
       " 'effect': 378,\n",
       " 'effects': 379,\n",
       " 'efforts': 380,\n",
       " 'egregious': 381,\n",
       " 'eighteenth-century': 382,\n",
       " 'elbow': 383,\n",
       " 'elegant': 384,\n",
       " 'else': 385,\n",
       " 'embarrassed': 386,\n",
       " 'enabled': 387,\n",
       " 'end': 388,\n",
       " 'endless': 389,\n",
       " 'enjoy': 390,\n",
       " 'enlightenment': 391,\n",
       " 'enough': 392,\n",
       " 'ensuing': 393,\n",
       " 'equally': 394,\n",
       " 'equanimity': 395,\n",
       " 'escape': 396,\n",
       " 'established': 397,\n",
       " 'etching': 398,\n",
       " 'even': 399,\n",
       " 'event': 400,\n",
       " 'ever': 401,\n",
       " 'everlasting': 402,\n",
       " 'every': 403,\n",
       " 'exasperated': 404,\n",
       " 'except': 405,\n",
       " 'excuse': 406,\n",
       " 'excusing': 407,\n",
       " 'existed': 408,\n",
       " 'expected': 409,\n",
       " 'exquisite': 410,\n",
       " 'exquisitely': 411,\n",
       " 'extenuation': 412,\n",
       " 'exterminating': 413,\n",
       " 'extracting': 414,\n",
       " 'eye': 415,\n",
       " 'eyebrows': 416,\n",
       " 'eyes': 417,\n",
       " 'face': 418,\n",
       " 'faces': 419,\n",
       " 'fact': 420,\n",
       " 'faded': 421,\n",
       " 'failed': 422,\n",
       " 'failure': 423,\n",
       " 'fair': 424,\n",
       " 'faith': 425,\n",
       " 'false': 426,\n",
       " 'familiar': 427,\n",
       " 'famille-verte': 428,\n",
       " 'fancy': 429,\n",
       " 'fashionable': 430,\n",
       " 'fate': 431,\n",
       " 'feather': 432,\n",
       " 'feet': 433,\n",
       " 'fell': 434,\n",
       " 'fellow': 435,\n",
       " 'felt': 436,\n",
       " 'few': 437,\n",
       " 'fewer': 438,\n",
       " 'finality': 439,\n",
       " 'find': 440,\n",
       " 'fingers': 441,\n",
       " 'first': 442,\n",
       " 'fit': 443,\n",
       " 'fitting': 444,\n",
       " 'five': 445,\n",
       " 'flash': 446,\n",
       " 'flashed': 447,\n",
       " 'florid': 448,\n",
       " 'flowers': 449,\n",
       " 'fluently': 450,\n",
       " 'flung': 451,\n",
       " 'follow': 452,\n",
       " 'followed': 453,\n",
       " 'fond': 454,\n",
       " 'footstep': 455,\n",
       " 'for': 456,\n",
       " 'forced': 457,\n",
       " 'forcing': 458,\n",
       " 'forehead': 459,\n",
       " 'foreign': 460,\n",
       " 'foreseen': 461,\n",
       " 'forgive': 462,\n",
       " 'forgotten': 463,\n",
       " 'form': 464,\n",
       " 'formed': 465,\n",
       " 'forming': 466,\n",
       " 'forward': 467,\n",
       " 'fostered': 468,\n",
       " 'found': 469,\n",
       " 'foundations': 470,\n",
       " 'fragment': 471,\n",
       " 'fragments': 472,\n",
       " 'frame': 473,\n",
       " 'frames': 474,\n",
       " 'frequently': 475,\n",
       " 'friend': 476,\n",
       " 'from': 477,\n",
       " 'full': 478,\n",
       " 'fullest': 479,\n",
       " 'furiously': 480,\n",
       " 'furrowed': 481,\n",
       " 'garlanded': 482,\n",
       " 'garlands': 483,\n",
       " 'gave': 484,\n",
       " 'genial': 485,\n",
       " 'genius': 486,\n",
       " 'gesture': 487,\n",
       " 'get': 488,\n",
       " 'getting': 489,\n",
       " 'give': 490,\n",
       " 'given': 491,\n",
       " 'glad': 492,\n",
       " 'glanced': 493,\n",
       " 'glimpse': 494,\n",
       " 'gloried': 495,\n",
       " 'glory': 496,\n",
       " 'go': 497,\n",
       " 'going': 498,\n",
       " 'gone': 499,\n",
       " 'good': 500,\n",
       " 'good-breeding': 501,\n",
       " 'good-humoured': 502,\n",
       " 'got': 503,\n",
       " 'grace': 504,\n",
       " 'gradually': 505,\n",
       " 'gray': 506,\n",
       " 'grayish': 507,\n",
       " 'great': 508,\n",
       " 'greatest': 509,\n",
       " 'greatness': 510,\n",
       " 'grew': 511,\n",
       " 'groping': 512,\n",
       " 'growing': 513,\n",
       " 'had': 514,\n",
       " 'hadn': 515,\n",
       " 'hair': 516,\n",
       " 'half': 517,\n",
       " 'half-light': 518,\n",
       " 'half-mechanically': 519,\n",
       " 'hall': 520,\n",
       " 'hand': 521,\n",
       " 'hands': 522,\n",
       " 'handsome': 523,\n",
       " 'hanging': 524,\n",
       " 'happen': 525,\n",
       " 'happened': 526,\n",
       " 'hard': 527,\n",
       " 'hardly': 528,\n",
       " 'has': 529,\n",
       " 'have': 530,\n",
       " 'haven': 531,\n",
       " 'having': 532,\n",
       " 'he': 533,\n",
       " 'head': 534,\n",
       " 'hear': 535,\n",
       " 'heard': 536,\n",
       " 'heart': 537,\n",
       " 'height': 538,\n",
       " 'her': 539,\n",
       " 'here': 540,\n",
       " 'hermit': 541,\n",
       " 'herself': 542,\n",
       " 'hesitations': 543,\n",
       " 'hide': 544,\n",
       " 'high': 545,\n",
       " 'him': 546,\n",
       " 'himself': 547,\n",
       " 'hint': 548,\n",
       " 'his': 549,\n",
       " 'history': 550,\n",
       " 'holding': 551,\n",
       " 'home': 552,\n",
       " 'honour': 553,\n",
       " 'hooded': 554,\n",
       " 'hostess': 555,\n",
       " 'hot-house': 556,\n",
       " 'hour': 557,\n",
       " 'hours': 558,\n",
       " 'house': 559,\n",
       " 'how': 560,\n",
       " 'hung': 561,\n",
       " 'husband': 562,\n",
       " 'idea': 563,\n",
       " 'idle': 564,\n",
       " 'idling': 565,\n",
       " 'if': 566,\n",
       " 'immediately': 567,\n",
       " 'in': 568,\n",
       " 'incense': 569,\n",
       " 'indifferent': 570,\n",
       " 'inevitable': 571,\n",
       " 'inevitably': 572,\n",
       " 'inflexible': 573,\n",
       " 'insensible': 574,\n",
       " 'insignificant': 575,\n",
       " 'instinctively': 576,\n",
       " 'instructive': 577,\n",
       " 'interesting': 578,\n",
       " 'into': 579,\n",
       " 'ironic': 580,\n",
       " 'irony': 581,\n",
       " 'irrelevance': 582,\n",
       " 'irrevocable': 583,\n",
       " 'is': 584,\n",
       " 'it': 585,\n",
       " 'its': 586,\n",
       " 'itself': 587,\n",
       " 'jardiniere': 588,\n",
       " 'jealousy': 589,\n",
       " 'just': 590,\n",
       " 'keep': 591,\n",
       " 'kept': 592,\n",
       " 'kind': 593,\n",
       " 'knees': 594,\n",
       " 'knew': 595,\n",
       " 'know': 596,\n",
       " 'known': 597,\n",
       " 'laid': 598,\n",
       " 'lair': 599,\n",
       " 'landing': 600,\n",
       " 'language': 601,\n",
       " 'last': 602,\n",
       " 'late': 603,\n",
       " 'later': 604,\n",
       " 'latter': 605,\n",
       " 'laugh': 606,\n",
       " 'laughed': 607,\n",
       " 'lay': 608,\n",
       " 'leading': 609,\n",
       " 'lean': 610,\n",
       " 'learned': 611,\n",
       " 'least': 612,\n",
       " 'leathery': 613,\n",
       " 'leave': 614,\n",
       " 'led': 615,\n",
       " 'left': 616,\n",
       " 'leisure': 617,\n",
       " 'lends': 618,\n",
       " 'lent': 619,\n",
       " 'let': 620,\n",
       " 'lies': 621,\n",
       " 'life': 622,\n",
       " 'life-likeness': 623,\n",
       " 'lift': 624,\n",
       " 'lifted': 625,\n",
       " 'light': 626,\n",
       " 'lightly': 627,\n",
       " 'like': 628,\n",
       " 'liked': 629,\n",
       " 'line': 630,\n",
       " 'lines': 631,\n",
       " 'lingered': 632,\n",
       " 'lips': 633,\n",
       " 'lit': 634,\n",
       " 'little': 635,\n",
       " 'live': 636,\n",
       " 'll': 637,\n",
       " 'loathing': 638,\n",
       " 'long': 639,\n",
       " 'longed': 640,\n",
       " 'longer': 641,\n",
       " 'look': 642,\n",
       " 'looked': 643,\n",
       " 'looking': 644,\n",
       " 'lose': 645,\n",
       " 'loss': 646,\n",
       " 'lounging': 647,\n",
       " 'lovely': 648,\n",
       " 'lucky': 649,\n",
       " 'lump': 650,\n",
       " 'luncheon-table': 651,\n",
       " 'luxury': 652,\n",
       " 'lying': 653,\n",
       " 'made': 654,\n",
       " 'make': 655,\n",
       " 'man': 656,\n",
       " 'manage': 657,\n",
       " 'managed': 658,\n",
       " 'mantel-piece': 659,\n",
       " 'marble': 660,\n",
       " 'married': 661,\n",
       " 'may': 662,\n",
       " 'me': 663,\n",
       " 'meant': 664,\n",
       " 'mediocrity': 665,\n",
       " 'medium': 666,\n",
       " 'mentioned': 667,\n",
       " 'mere': 668,\n",
       " 'merely': 669,\n",
       " 'met': 670,\n",
       " 'might': 671,\n",
       " 'mighty': 672,\n",
       " 'millionaire': 673,\n",
       " 'mine': 674,\n",
       " 'minute': 675,\n",
       " 'minutes': 676,\n",
       " 'mirrors': 677,\n",
       " 'modest': 678,\n",
       " 'modesty': 679,\n",
       " 'moment': 680,\n",
       " 'money': 681,\n",
       " 'monumental': 682,\n",
       " 'mood': 683,\n",
       " 'morbidly': 684,\n",
       " 'more': 685,\n",
       " 'most': 686,\n",
       " 'mourn': 687,\n",
       " 'mourned': 688,\n",
       " 'moustache': 689,\n",
       " 'moved': 690,\n",
       " 'much': 691,\n",
       " 'muddling': 692,\n",
       " 'multiplied': 693,\n",
       " 'murmur': 694,\n",
       " 'muscles': 695,\n",
       " 'must': 696,\n",
       " 'my': 697,\n",
       " 'myself': 698,\n",
       " 'mysterious': 699,\n",
       " 'naive': 700,\n",
       " 'near': 701,\n",
       " 'nearly': 702,\n",
       " 'negatived': 703,\n",
       " 'nervous': 704,\n",
       " 'nervousness': 705,\n",
       " 'neutral': 706,\n",
       " 'never': 707,\n",
       " 'next': 708,\n",
       " 'no': 709,\n",
       " 'none': 710,\n",
       " 'not': 711,\n",
       " 'note': 712,\n",
       " 'nothing': 713,\n",
       " 'now': 714,\n",
       " 'nymphs': 715,\n",
       " 'oak': 716,\n",
       " 'obituary': 717,\n",
       " 'object': 718,\n",
       " 'objects': 719,\n",
       " 'occurred': 720,\n",
       " 'oddly': 721,\n",
       " 'of': 722,\n",
       " 'off': 723,\n",
       " 'often': 724,\n",
       " 'oh': 725,\n",
       " 'old': 726,\n",
       " 'on': 727,\n",
       " 'once': 728,\n",
       " 'one': 729,\n",
       " 'ones': 730,\n",
       " 'only': 731,\n",
       " 'onto': 732,\n",
       " 'open': 733,\n",
       " 'or': 734,\n",
       " 'other': 735,\n",
       " 'our': 736,\n",
       " 'ourselves': 737,\n",
       " 'out': 738,\n",
       " 'outline': 739,\n",
       " 'oval': 740,\n",
       " 'over': 741,\n",
       " 'own': 742,\n",
       " 'packed': 743,\n",
       " 'paid': 744,\n",
       " 'paint': 745,\n",
       " 'painted': 746,\n",
       " 'painter': 747,\n",
       " 'painting': 748,\n",
       " 'pale': 749,\n",
       " 'paled': 750,\n",
       " 'palm-trees': 751,\n",
       " 'panel': 752,\n",
       " 'panelling': 753,\n",
       " 'pardonable': 754,\n",
       " 'pardoned': 755,\n",
       " 'part': 756,\n",
       " 'passages': 757,\n",
       " 'passing': 758,\n",
       " 'past': 759,\n",
       " 'pastels': 760,\n",
       " 'pathos': 761,\n",
       " 'patient': 762,\n",
       " 'people': 763,\n",
       " 'perceptible': 764,\n",
       " 'perfect': 765,\n",
       " 'persistence': 766,\n",
       " 'persuasively': 767,\n",
       " 'phrase': 768,\n",
       " 'picture': 769,\n",
       " 'pictures': 770,\n",
       " 'pines': 771,\n",
       " 'pink': 772,\n",
       " 'place': 773,\n",
       " 'placed': 774,\n",
       " 'plain': 775,\n",
       " 'platitudes': 776,\n",
       " 'pleased': 777,\n",
       " 'pockets': 778,\n",
       " 'point': 779,\n",
       " 'poised': 780,\n",
       " 'poor': 781,\n",
       " 'portrait': 782,\n",
       " 'posing': 783,\n",
       " 'possessed': 784,\n",
       " 'poverty': 785,\n",
       " 'predicted': 786,\n",
       " 'preliminary': 787,\n",
       " 'presenting': 788,\n",
       " 'prestidigitation': 789,\n",
       " 'pretty': 790,\n",
       " 'previous': 791,\n",
       " 'price': 792,\n",
       " 'pride': 793,\n",
       " 'princely': 794,\n",
       " 'prism': 795,\n",
       " 'problem': 796,\n",
       " 'proclaiming': 797,\n",
       " 'prodigious': 798,\n",
       " 'profusion': 799,\n",
       " 'protest': 800,\n",
       " 'prove': 801,\n",
       " 'public': 802,\n",
       " 'purblind': 803,\n",
       " 'purely': 804,\n",
       " 'pushed': 805,\n",
       " 'put': 806,\n",
       " 'qualities': 807,\n",
       " 'quality': 808,\n",
       " 'queerly': 809,\n",
       " 'question': 810,\n",
       " 'quickly': 811,\n",
       " 'quietly': 812,\n",
       " 'quite': 813,\n",
       " 'quote': 814,\n",
       " 'rain': 815,\n",
       " 'raised': 816,\n",
       " 'random': 817,\n",
       " 'rather': 818,\n",
       " 're': 819,\n",
       " 'real': 820,\n",
       " 'really': 821,\n",
       " 'reared': 822,\n",
       " 'reason': 823,\n",
       " 'reassurance': 824,\n",
       " 'recovering': 825,\n",
       " 'recreated': 826,\n",
       " 'reflected': 827,\n",
       " 'reflection': 828,\n",
       " 'regrets': 829,\n",
       " 'relatively': 830,\n",
       " 'remained': 831,\n",
       " 'remember': 832,\n",
       " 'reminded': 833,\n",
       " 'repeating': 834,\n",
       " 'represented': 835,\n",
       " 'reproduction': 836,\n",
       " 'resented': 837,\n",
       " 'resolve': 838,\n",
       " 'resources': 839,\n",
       " 'rest': 840,\n",
       " 'rich': 841,\n",
       " 'ridiculous': 842,\n",
       " 'robbed': 843,\n",
       " 'romantic': 844,\n",
       " 'room': 845,\n",
       " 'rose': 846,\n",
       " 'rs': 847,\n",
       " 'rule': 848,\n",
       " 'run': 849,\n",
       " 's': 850,\n",
       " 'said': 851,\n",
       " 'same': 852,\n",
       " 'satisfaction': 853,\n",
       " 'savour': 854,\n",
       " 'saw': 855,\n",
       " 'say': 856,\n",
       " 'saying': 857,\n",
       " 'says': 858,\n",
       " 'scorn': 859,\n",
       " 'scornful': 860,\n",
       " 'secret': 861,\n",
       " 'see': 862,\n",
       " 'seemed': 863,\n",
       " 'seen': 864,\n",
       " 'self-confident': 865,\n",
       " 'send': 866,\n",
       " 'sensation': 867,\n",
       " 'sensitive': 868,\n",
       " 'sent': 869,\n",
       " 'serious': 870,\n",
       " 'set': 871,\n",
       " 'sex': 872,\n",
       " 'shade': 873,\n",
       " 'shaking': 874,\n",
       " 'shall': 875,\n",
       " 'she': 876,\n",
       " 'shirked': 877,\n",
       " 'short': 878,\n",
       " 'should': 879,\n",
       " 'shoulder': 880,\n",
       " 'shoulders': 881,\n",
       " 'show': 882,\n",
       " 'showed': 883,\n",
       " 'showy': 884,\n",
       " 'shrug': 885,\n",
       " 'shrugged': 886,\n",
       " 'sight': 887,\n",
       " 'sign': 888,\n",
       " 'silent': 889,\n",
       " 'silver': 890,\n",
       " 'similar': 891,\n",
       " 'simpleton': 892,\n",
       " 'simplifications': 893,\n",
       " 'simply': 894,\n",
       " 'since': 895,\n",
       " 'single': 896,\n",
       " 'sitter': 897,\n",
       " 'sitters': 898,\n",
       " 'sketch': 899,\n",
       " 'skill': 900,\n",
       " 'slight': 901,\n",
       " 'slightly': 902,\n",
       " 'slowly': 903,\n",
       " 'small': 904,\n",
       " 'smile': 905,\n",
       " 'smiling': 906,\n",
       " 'sneer': 907,\n",
       " 'so': 908,\n",
       " 'solace': 909,\n",
       " 'some': 910,\n",
       " 'somebody': 911,\n",
       " 'something': 912,\n",
       " 'spacious': 913,\n",
       " 'spaniel': 914,\n",
       " 'speaking-tubes': 915,\n",
       " 'speculations': 916,\n",
       " 'spite': 917,\n",
       " 'splash': 918,\n",
       " 'square': 919,\n",
       " 'stairs': 920,\n",
       " 'stammer': 921,\n",
       " 'stand': 922,\n",
       " 'standing': 923,\n",
       " 'started': 924,\n",
       " 'stay': 925,\n",
       " 'still': 926,\n",
       " 'stocked': 927,\n",
       " 'stood': 928,\n",
       " 'stopped': 929,\n",
       " 'stopping': 930,\n",
       " 'straddling': 931,\n",
       " 'straight': 932,\n",
       " 'strain': 933,\n",
       " 'straining': 934,\n",
       " 'strange': 935,\n",
       " 'straw': 936,\n",
       " 'stream': 937,\n",
       " 'stroke': 938,\n",
       " 'strokes': 939,\n",
       " 'strolled': 940,\n",
       " 'strongest': 941,\n",
       " 'strongly': 942,\n",
       " 'struck': 943,\n",
       " 'studio': 944,\n",
       " 'stuff': 945,\n",
       " 'subject': 946,\n",
       " 'substantial': 947,\n",
       " 'suburban': 948,\n",
       " 'such': 949,\n",
       " 'suddenly': 950,\n",
       " 'suffered': 951,\n",
       " 'sugar': 952,\n",
       " 'suggested': 953,\n",
       " 'sunburn': 954,\n",
       " 'sunburnt': 955,\n",
       " 'sunlit': 956,\n",
       " 'superb': 957,\n",
       " 'sure': 958,\n",
       " 'surest': 959,\n",
       " 'surface': 960,\n",
       " 'surprise': 961,\n",
       " 'surprised': 962,\n",
       " 'surrounded': 963,\n",
       " 'suspected': 964,\n",
       " 'sweetly': 965,\n",
       " 'sweetness': 966,\n",
       " 'swelling': 967,\n",
       " 'swept': 968,\n",
       " 'swum': 969,\n",
       " 't': 970,\n",
       " 'table': 971,\n",
       " 'take': 972,\n",
       " 'taken': 973,\n",
       " 'talking': 974,\n",
       " 'tea': 975,\n",
       " 'tears': 976,\n",
       " 'technicalities': 977,\n",
       " 'technique': 978,\n",
       " 'tell': 979,\n",
       " 'tells': 980,\n",
       " 'tempting': 981,\n",
       " 'terra-cotta': 982,\n",
       " 'terrace': 983,\n",
       " 'terraces': 984,\n",
       " 'terribly': 985,\n",
       " 'than': 986,\n",
       " 'that': 987,\n",
       " 'the': 988,\n",
       " 'their': 989,\n",
       " 'them': 990,\n",
       " 'then': 991,\n",
       " 'there': 992,\n",
       " 'therefore': 993,\n",
       " 'they': 994,\n",
       " 'thin': 995,\n",
       " 'thing': 996,\n",
       " 'things': 997,\n",
       " 'think': 998,\n",
       " 'this': 999,\n",
       " ...}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vocab = {token:integer for integer,token in enumerate(all_words)}\n",
    "vocab"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "4a8bfb14-aadb-4add-9e23-ecc3e060e05e",
   "metadata": {},
   "outputs": [],
   "source": [
    "class SimpleTokenizerV1:\n",
    "    def __init__(self, vocab):\n",
    "        self.str_to_int = vocab\n",
    "        self.int_to_str = {i:s for s,i in vocab.items()}\n",
    "    def encode(self, text):\n",
    "        preprocessed = re.split(r'([,.;:?_!\"()\\']|--|\\s)', text)\n",
    "        preprocessed = [item.strip() for item in preprocessed if item.strip()]\n",
    "        ids = [self.str_to_int[s] for s in preprocessed]\n",
    "        return ids\n",
    "    def decode(self, ids):\n",
    "        text = \" \".join(self.int_to_str[i] for i in ids)\n",
    "        text = re.sub(r'\\s+([,.?!\"()\\'])', r'\\1', text)\n",
    "        return text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "1657bbe8-e808-41f2-a084-8a894ad20a95",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 56, 2, 850, 988, 602, 533, 746, 5, 1126, 596, 5, 1, 67, 7, 38, 851, 1108, 754, 793, 7]\n"
     ]
    }
   ],
   "source": [
    "tokenizer = SimpleTokenizerV1(vocab)\n",
    "\n",
    "text = \"\"\"\"It's the last he painted, you know,\" \n",
    "           Mrs. Gisburn said with pardonable pride.\"\"\"\n",
    "ids = tokenizer.encode(text)\n",
    "print(ids)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
